
%macro solve_owners_inner(year=);

	proc printto new log = "&logdir./solve_owners_inner_&year..txt";
	run;
	

	proc sort data = rlib.f941(keep=firm_id)
		out = iter0
		nodupkey;
		by firm_id;
	run;
	
	data iter0;
		set iter0;
		own_share_link = 1;
		owner_tin = firm_id;
	run;
	
	
	
	/********************************
	*	Iterate to get mapping		*
	*	from firm_id to parents		*
	********************************/
	
	/* not worried about getting overall parent */
	%let setlist = iter0;
	
	%do i = 1 %to 5;
		%iter(i=&i,year=&year);
		%let setlist = &setlist iter&i;
	%end;
	
	data appended;
		set &setlist;
	run;
	proc means data = appended nway noprint;
		class firm_id owner_tin;
		output out = tot_own(drop=_type_ _freq_)
			sum(own_share_link) = own_share_link;
	run;
	
	data tot_own;
		set tot_own;
		own_share_link = min(1,own_share_link);
	run;
	
		
	/********************************
	*	Get individual owners		*
	********************************/
	proc sql noprint;
		create table ind_owners as
		select
			a.firm_id,
			b.person_id,
			sum(b.own_share*a.own_share_link) as own_share
		from tot_own as a
			inner join rlib.firm_owners as b
				on(a.owner_tin = b.firm_id)
			where b.year = &year
			and b.own_share > 0.05
		group by a.firm_id, b.person_id;
	run;
	
	data ind_owners;
		set ind_owners;
		own_share = min(own_share,1);
	run;
	

	/************************************************
	*		Normalize ownership share  		*
	************************************************/

	proc means data = ind_owners nway noprint;
		class firm_id;
		output out = norm_own(drop=_type_ _freq_)
			sum(own_share) = norm_own;
	run;
	proc sort data = ind_owners;
		by firm_id;
	run;
	
	data ind_owners_&year;
		merge ind_owners norm_own;
		by firm_id;
		own_share = own_share/norm_own;
		keep person_id firm_id own_share;
	run;
	
	proc sort data = ind_owners_&year;
		by firm_id person_id;
	run;
	
	
	proc sort data = ind_owners_&year.(keep=firm_id) nodupkey
		out = found_firms_&year;
		by firm_id;
	run;
	

	
	proc printto;
	run;

%mend;



%macro iter(i=,year=);

	%let ilag = %eval(&i-1);
	
	proc sql noprint;
		create table iter&i as 
		select
			a.firm_id,
			b.parent_tin as owner_tin,
			a.own_share_link*b.own_share_link as own_share_link
		from iter&ilag as a
			inner join rlib.links as b
				on(a.owner_tin = b.child_tin)
		where a.own_share_link*b.own_share_link > 0.2
			and b.year = &year;
	quit;



%mend;



%macro solve_owners;


	%solve_owners_inner(year=2019);
	%solve_owners_inner(year=2020);

	proc printto new log = "&logdir./solve_owners.txt";
	run;
	
	data firms;
		merge found_firms_2019(in=a) found_firms_2020(in=b);
		by firm_id;
		use_2019 = b = 0;
		keep firm_id use_2019;
	run;
	
	data use_2019;
		merge ind_owners_2019 firms(where=(use_2019) in=b);
		by firm_id;
		if b;
	run;
	data use_2020;
		merge ind_owners_2020 firms(where=(use_2019 = 0) in=b);
		by firm_id;
		if b;
	run;
	
	data rlib.ind_owners;
		set use_2019 use_2020;
	run;
	proc sort data = rlib.ind_owners;
		by firm_id person_id;
	run;
	
		
	/* share found */
	title "Number found";
	proc means data = rlib.ind_owners sum;
		var own_share;
	run;
	
	
	
	
	proc printto;
	run;
%mend;
